package visual;

import java.awt.Dimension;
import javax.swing.JPanel;
import java.awt.GridBagLayout;
import java.awt.BorderLayout;
import javax.swing.JLabel;
import java.awt.Font;
import java.awt.Rectangle;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Collection;

import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.JComboBox;
import javax.swing.JCheckBox;
import javax.swing.table.DefaultTableModel;

import negocio.GerenciadorDependente;

import modelo.Dependente;
import modelo.Funcionario;

public class FrmCadastroDependente extends FormularioBase {

	private Funcionario funcionario;  //  @jve:decl-index=0:
	private Dependente dependente;  //  @jve:decl-index=0:
	
	private JPanel jPanel = null;
	private JLabel jLabel = null;
	private BarraDeControle barraDeControle = null;
	private JPanel jPanel1 = null;
	private JLabel nomeFuncionarioLabel = null;
	private JScrollPane jScrollPane = null;
	private JTable dependenteTable = null;
	private JLabel dependentesLabel = null;
	private JLabel jLabel2 = null;
	private JTextField nomeTextField = null;
	private JLabel jLabel3 = null;
	private JTextField dataNacimentoTextField = null;
	private JLabel jLabel4 = null;
	private JComboBox sexoComboBox = null;
	private JLabel jLabel5 = null;
	private JTextField localNacimentoTextField = null;
	private JLabel jLabel6 = null;
	private JTextField parentescoTextField = null;
	private JCheckBox incideParaIRPFCheckBox = null;
	private JCheckBox deficienteFisicoCheckBox = null;
	private JCheckBox univercitarioCheckBox = null;
	/**
	 * This method initializes 
	 * 
	 */
	public FrmCadastroDependente() {
		super(null);
		initialize();
	}

	/**
	 * This method initializes this
	 * 
	 */
	private void initialize() {
        this.setSize(new Dimension(695, 382));
        this.setContentPane(getJPanel());
        this.setTitle("Cadastro de dependentes");
        this.addWindowListener(new java.awt.event.WindowAdapter() {
        	public void windowOpened(java.awt.event.WindowEvent e) {
        		System.out.println("windowOpened()"); // TODO Auto-generated Event stub windowOpened()
                configuraNovo();
                nomeFuncionarioLabel.setText("Dependentes do funcionario " + funcionario.getNome());
                pesquisar();
       	}
        });
        getBarraDeControle().setFormulario(this);
	}

	@Override
	public void apagarFormulario() {
		// TODO Auto-generated method stub
		int opcao = JOptionPane.showConfirmDialog(this, "Deseja excluir esse dependente", "Atenção",
				JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE);
		if(opcao == JOptionPane.YES_OPTION){
			GerenciadorDependente gerenciadorDependente = new GerenciadorDependente();
			gerenciadorDependente.removerDependente(dependente);
			System.out.println("\ndependente id = " + dependente.getId());
			this.dispose();
		}
	}

	@Override
	public void carregaFormulario() {
		// TODO Auto-generated method stub
		GerenciadorDependente gerenciadorDependente = new GerenciadorDependente();
		dependente = gerenciadorDependente.buscarDependente(getId());
		
		nomeTextField.setText(dependente.getNome());
		
		SimpleDateFormat sdf = new SimpleDateFormat("dd/mm/yyyy");
		dataNacimentoTextField.setText(sdf.format(dependente.getDataNacimento()));
		
		localNacimentoTextField.setText(dependente.getLocalNacimento());
		parentescoTextField.setText(dependente.getParentesco());
		
		incideParaIRPFCheckBox.setSelected(dependente.isIncideParaIRPF());
		deficienteFisicoCheckBox.setSelected(dependente.isDeficienteFisico());
		univercitarioCheckBox.setSelected(dependente.isUniversitario());
		

	}

	@Override
	public void salvaFormulario() {
		// TODO Auto-generated method stub
		setDependente( new Dependente() );
		Dependente d = getDependente();
		
		try {
			d.setId(getId());
			d.setFuncionario( funcionario );
			
			d.setNome( nomeTextField.getText() );
			
			SimpleDateFormat sdf = new SimpleDateFormat("dd/mm/yyyy");
			d.setDataNacimento(sdf.parse(dataNacimentoTextField.getText()));
			
			d.setLocalNacimento(localNacimentoTextField.getText());
			d.setParentesco(parentescoTextField.getText());
			
			d.setIncideParaIRPF(incideParaIRPFCheckBox.isSelected());
			d.setDeficienteFisico(deficienteFisicoCheckBox.isSelected());
			d.setUniversitario(univercitarioCheckBox.isSelected());
			
			GerenciadorDependente gerenciadorDependente = new GerenciadorDependente();
			gerenciadorDependente.salvarDependente(d);
			
			JOptionPane.showMessageDialog(this, "Dependende salvo com sucesso");
			
			dispose();
			
		} catch (ParseException e) {
			JOptionPane.showMessageDialog(this, "formato de data invalido");
		}
		

	}

	/**
	 * This method initializes jPanel	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJPanel() {
		if (jPanel == null) {
			jLabel = new JLabel();
			jLabel.setText("Cadastro de dependentes");
			jLabel.setFont(new Font("Dialog", Font.BOLD, 24));
			jPanel = new JPanel();
			jPanel.setLayout(new BorderLayout());
			jPanel.add(jLabel, BorderLayout.NORTH);
			jPanel.add(getBarraDeControle(), BorderLayout.SOUTH);
			jPanel.add(getJPanel1(), BorderLayout.CENTER);
		}
		return jPanel;
	}

	/**
	 * This method initializes barraDeControle	
	 * 	
	 * @return visual.BarraDeControle	
	 */
	private BarraDeControle getBarraDeControle() {
		if (barraDeControle == null) {
			barraDeControle = new BarraDeControle();
		}
		return barraDeControle;
	}

	/**
	 * This method initializes jPanel1	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJPanel1() {
		if (jPanel1 == null) {
			jLabel6 = new JLabel();
			jLabel6.setBounds(new Rectangle(346, 225, 86, 14));
			jLabel6.setText("Parentesco:");
			jLabel5 = new JLabel();
			jLabel5.setBounds(new Rectangle(15, 223, 142, 14));
			jLabel5.setText("Local de nacimento:");
			jLabel4 = new JLabel();
			jLabel4.setBounds(new Rectangle(570, 196, 43, 14));
			jLabel4.setText("Sexo:");
			jLabel3 = new JLabel();
			jLabel3.setBounds(new Rectangle(345, 195, 118, 14));
			jLabel3.setText("Data Nacimento:");
			jLabel2 = new JLabel();
			jLabel2.setBounds(new Rectangle(15, 195, 45, 14));
			jLabel2.setText("Nome:");
			dependentesLabel = new JLabel();
			dependentesLabel.setBounds(new Rectangle(15, 165, 101, 14));
			dependentesLabel.setText("Dependentes:");
			nomeFuncionarioLabel = new JLabel();
			nomeFuncionarioLabel.setBounds(new Rectangle(15, 15, 657, 14));
			nomeFuncionarioLabel.setText("Dependentes:");
			jPanel1 = new JPanel();
			jPanel1.setLayout(null);
			jPanel1.add(nomeFuncionarioLabel, null);
			jPanel1.add(getJScrollPane(), null);
			jPanel1.add(dependentesLabel, null);
			jPanel1.add(jLabel2, null);
			jPanel1.add(getNomeTextField(), null);
			jPanel1.add(jLabel3, null);
			jPanel1.add(getDataNacimentoTextField(), null);
			jPanel1.add(jLabel4, null);
			jPanel1.add(getSexoComboBox(), null);
			jPanel1.add(jLabel5, null);
			jPanel1.add(getLocalNacimentoTextField(), null);
			jPanel1.add(jLabel6, null);
			jPanel1.add(getParentescoTextField(), null);
			jPanel1.add(getIncideParaIRPFCheckBox(), null);
			jPanel1.add(getDeficienteFisicoCheckBox(), null);
			jPanel1.add(getUnivercitarioCheckBox(), null);
		}
		return jPanel1;
	}

	/**
	 * This method initializes jScrollPane	
	 * 	
	 * @return javax.swing.JScrollPane	
	 */
	private JScrollPane getJScrollPane() {
		if (jScrollPane == null) {
			jScrollPane = new JScrollPane();
			jScrollPane.setBounds(new Rectangle(15, 45, 661, 106));
			jScrollPane.setViewportView(getDependenteTable());
		}
		return jScrollPane;
	}

	/**
	 * This method initializes dependenteTable	
	 * 	
	 * @return javax.swing.JTable	
	 */
	private JTable getDependenteTable() {
		if (dependenteTable == null) {
			dependenteTable = new JTable();
			dependenteTable.addMouseListener(new java.awt.event.MouseAdapter() {
				public void mouseClicked(java.awt.event.MouseEvent e) {
					System.out.println("mouseClicked()"); // TODO Auto-generated Event stub mouseClicked()
					if(e.getClickCount() == 2 ){
						setId(Integer.parseInt(
								getDependenteTable().getValueAt(
										getDependenteTable().getSelectedRow(), 0).toString()));
						carregaFormulario();
						configuraEdicao(); 
					}
				}
			});
		}
		return dependenteTable;
	}

	/**
	 * This method initializes nomeTextField	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getNomeTextField() {
		if (nomeTextField == null) {
			nomeTextField = new JTextField();
			nomeTextField.setBounds(new Rectangle(75, 195, 256, 18));
		}
		return nomeTextField;
	}

	/**
	 * This method initializes dataNacimentoTextField	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getDataNacimentoTextField() {
		if (dataNacimentoTextField == null) {
			dataNacimentoTextField = new JTextField();
			dataNacimentoTextField.setBounds(new Rectangle(465, 195, 91, 18));
		}
		return dataNacimentoTextField;
	}

	/**
	 * This method initializes sexoComboBox	
	 * 	
	 * @return javax.swing.JComboBox	
	 */
	private JComboBox getSexoComboBox() {
		if (sexoComboBox == null) {
			sexoComboBox = new JComboBox();
			sexoComboBox.setBounds(new Rectangle(617, 196, 59, 23));
		}
		return sexoComboBox;
	}

	/**
	 * This method initializes localNacimentoTextField	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getLocalNacimentoTextField() {
		if (localNacimentoTextField == null) {
			localNacimentoTextField = new JTextField();
			localNacimentoTextField.setBounds(new Rectangle(165, 225, 166, 18));
		}
		return localNacimentoTextField;
	}

	/**
	 * This method initializes parentescoTextField	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getParentescoTextField() {
		if (parentescoTextField == null) {
			parentescoTextField = new JTextField();
			parentescoTextField.setBounds(new Rectangle(435, 225, 120, 18));
		}
		return parentescoTextField;
	}

	/**
	 * This method initializes incideParaIRPFCheckBox	
	 * 	
	 * @return javax.swing.JCheckBox	
	 */
	private JCheckBox getIncideParaIRPFCheckBox() {
		if (incideParaIRPFCheckBox == null) {
			incideParaIRPFCheckBox = new JCheckBox();
			incideParaIRPFCheckBox.setBounds(new Rectangle(15, 254, 145, 22));
			incideParaIRPFCheckBox.setText("Incide para IRPF?");
		}
		return incideParaIRPFCheckBox;
	}

	/**
	 * This method initializes deficienteFisicoCheckBox	
	 * 	
	 * @return javax.swing.JCheckBox	
	 */
	private JCheckBox getDeficienteFisicoCheckBox() {
		if (deficienteFisicoCheckBox == null) {
			deficienteFisicoCheckBox = new JCheckBox();
			deficienteFisicoCheckBox.setBounds(new Rectangle(196, 255, 145, 22));
			deficienteFisicoCheckBox.setText("Deficiente físico?");
		}
		return deficienteFisicoCheckBox;
	}

	/**
	 * This method initializes univercitarioCheckBox	
	 * 	
	 * @return javax.swing.JCheckBox	
	 */
	private JCheckBox getUnivercitarioCheckBox() {
		if (univercitarioCheckBox == null) {
			univercitarioCheckBox = new JCheckBox();
			univercitarioCheckBox.setBounds(new Rectangle(435, 255, 123, 22));
			univercitarioCheckBox.setText("Universitario?");
		}
		return univercitarioCheckBox;
	}
	@Override
	public void configuraNovo() {
		// TODO Auto-generated method stub
        getBarraDeControle().ativaBotoes(false, false, false, true, false, false);
        getBarraDeControle().removerBotoes(true, true, false, false, false, false);		
	}
	@Override
	public void configuraEdicao() {
		// TODO Auto-generated method stub
		getBarraDeControle().ativaBotoes(false, false, false, true, true, true);
		//getBarraDeControle().removerBotoes(, novo, salvar, cancelar, apagar)
		
	}

	public void pesquisar(){
		getDependenteTable().setModel(new MyTableModel(new String[][]{}, 
				new String[]{"ID","Nome","Parentesco"} ));
		
		MyTableModel model = (MyTableModel) getDependenteTable().getModel();
		
		GerenciadorDependente gerenciadorDependente = new GerenciadorDependente();
		Collection<Dependente> dependentes = gerenciadorDependente.listarDependentesPorFuncionario(funcionario);
		
		for(Dependente d: dependentes){
			model.addRow( new String[]{String.valueOf(d.getId()),d.getNome(),d.getParentesco()});
		}
	}
	


	public Funcionario getFuncionario() {
		return funcionario;
	}

	public void setFuncionario(Funcionario funcionario) {
		this.funcionario = funcionario;
	}

	public Dependente getDependente() {
		return dependente;
	}

	public void setDependente(Dependente dependente) {
		this.dependente = dependente;
	}

}  //  @jve:decl-index=0:visual-constraint="10,10"
